package cn.iocoder.yudao.module.system.service.a_picturesLog;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.a_manuscriptLog.vo.PicturesLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.a_pictures.vo.PicturesReqVO;
import cn.iocoder.yudao.module.system.config.a_gaojian.ConnectionConfig;
import com.trs.hybase.client.*;
import com.trs.hybase.client.params.SearchParams;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lxj
 */
@Service
@Validated
public class PicturesLogServiceImpl implements PicturesLogService {

    /**
     * 稿件日志表名
     */
    @Value("${hybase.dbname.picturesLog}")
    private String dbName;

    @Override
    public void insertPicturesLog(TRSConnection conn, PicturesReqVO picturesReqVO) {
        //创建记录对象
        List<TRSInputRecord> recordList = new ArrayList<>();
        TRSInputRecord record = new TRSInputRecord();
        //添加字段
        try {
            record.addColumn("id", IdUtil.fastSimpleUUID());
            record.addColumn("content", picturesReqVO.getContent());
            record.addColumn("author", picturesReqVO.getAuthor());
            record.addColumn("pic_create_time", picturesReqVO.getCreateTime());
            record.addColumn("keyword", picturesReqVO.getKeyword());
            record.addColumn("suffix", picturesReqVO.getSuffix());
            record.addColumn("pic_resolution", picturesReqVO.getPicResolution());
            record.addColumn("source", picturesReqVO.getSource());
            record.addColumn("category", picturesReqVO.getCategory());
            record.addColumn("url", picturesReqVO.getUrl());
            record.addColumn("version", picturesReqVO.getVersion());
            record.addColumn("watermark", picturesReqVO.getWatermark());
            record.addColumn("height", picturesReqVO.getHeight());
            record.addColumn("width", picturesReqVO.getWidth());
            record.addColumn("fileName", picturesReqVO.getFileName());
            record.addColumn("create_time", picturesReqVO.getLastModificationTime());
            record.addColumn("create_user_id", WebFrameworkUtils.getLoginUserId().toString());
            record.addColumn("relevanceId", picturesReqVO.getId());
            recordList.add(record);
            record.getUid();
            //执行插入记录（暂时在内存中）
            conn.executeInsert(dbName, recordList);
        } catch (TRSException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Boolean findExistByRelevanceId(TRSConnection conn, String relevanceId) {
        //设置返回结果字段
        TRSResultSet resultSet;
        String whereStr = "relevanceId:" + relevanceId;
        try {
            //在"pictures"数据库里检索
            resultSet = conn.executeSelect(dbName, whereStr, 0, 1, new SearchParams());
            if (resultSet != null) {
                if (resultSet.getNumFound() > 0) {
                    return true;
                }
            }
        } catch (TRSException e) {
            System.out.println("ErrorCode:" + e.getErrorCode());
            System.out.println("ErrorString:" + e.getErrorString());
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return false;
    }

    @Override
    public PageResult<PicturesLogPageReqVO> findPageByRelevanceId(PicturesLogPageReqVO pageReqVO) {
        PageResult<PicturesLogPageReqVO> pageResult = new PageResult<>();
        List<PicturesLogPageReqVO> picturesReqVOList = new ArrayList<>();
        //创建与服务器的连接实例
        TRSConnection conn = ConnectionConfig.getTRSConnection();
        SearchParams param = new SearchParams();
        //设置结果排序方式为创建时间排序
        param.setSortMethod("+version");
        //设置返回结果字段
        param.setReadColumns("id;content;author;pic_create_time;keyword;suffix;pic_resolution;source;category;url;version;watermark;height;width;create_time;create_user_id;fileName;relevanceId");
        TRSResultSet resultSet;
        String whereStr = "relevanceId:" + pageReqVO.getRelevanceId();

        try {
            //在"picturesLog"数据库里检索
            resultSet = conn.executeSelect(dbName, whereStr, ((pageReqVO.getPageNo() - 1) * pageReqVO.getPageSize()), pageReqVO.getPageSize(), param);
            if (resultSet != null) {
                for (int i = 0; i < resultSet.size(); i++) {
                    resultSet.moveNext();
                    TRSRecord re = resultSet.get();
                    PicturesLogPageReqVO picturesLogPageReqVO = new PicturesLogPageReqVO();
                    picturesLogPageReqVO.setId(re.getString("id"));
                    picturesLogPageReqVO.setContent(re.getString("content"));
                    picturesLogPageReqVO.setAuthor(re.getString("author"));
                    picturesLogPageReqVO.setPicCreateTime(re.getString("pic_create_time"));
                    picturesLogPageReqVO.setKeyword(re.getString("keyword"));
                    picturesLogPageReqVO.setSuffix(re.getString("suffix"));
                    picturesLogPageReqVO.setPicResolution(re.getString("pic_resolution"));
                    picturesLogPageReqVO.setSource(re.getString("source"));
                    picturesLogPageReqVO.setCategory(re.getString("category"));
                    picturesLogPageReqVO.setUrl(re.getString("url"));
                    picturesLogPageReqVO.setVersion(re.getInt("version"));
                    picturesLogPageReqVO.setWatermark(re.getInt("watermark"));
                    picturesLogPageReqVO.setHeight(re.getInt("height"));
                    picturesLogPageReqVO.setWidth(re.getInt("width"));
                    picturesLogPageReqVO.setCreateTime(re.getString("create_time"));
                    picturesLogPageReqVO.setCreateUserId(re.getString("create_user_id"));
                    picturesLogPageReqVO.setFileName(re.getString("fileName"));
                    picturesLogPageReqVO.setRelevanceId(re.getString("relevanceId"));
                    picturesReqVOList.add(picturesLogPageReqVO);
                }

                pageResult.setTotal(resultSet.getNumFound());
                pageResult.setList(picturesReqVOList);

            }
        } catch (TRSException e) {
            System.out.println("ErrorCode:" + e.getErrorCode());
            System.out.println("ErrorString:" + e.getErrorString());
        } finally {
            if (conn != null) {
                conn.close();
            }
        }
        return pageResult;
    }


}
